package com.gsm.service.impl;

import com.gsm.dao.WebCourseDao;
import com.gsm.entity.Result;
import com.gsm.po.Chapter;
import com.gsm.service.WebCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WebCourseServiceImpl implements WebCourseService {
    @Autowired
    private WebCourseDao webCourseDao;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result selectChaptersCoursesBySubjectId(Long subjectId) {
        List<Chapter> list = null;
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        list = (List<Chapter>) ops.get("web:chapters:courses:id:" + subjectId);
        if (list != null) {
            return Result.success(list);
        }
        list = webCourseDao.selectChaptersCoursesBySubjectId(subjectId);
        ops.set("web:chapters:courses:id:" + subjectId, list);
        return Result.success(list);
    }
}
